﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing.Design;
using System.Linq;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace SCS.Web.UI.WebControls
{
    /// <summary>
    /// Represents a data binding for mapping a data element in the data source to a property of a <see cref="HierListItem"/>.
    /// </summary>
    [DefaultProperty("TextField"), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class HierListItemBinding : IStateManager, ICloneable, IDataSourceViewSchemaAccessor
    {
        #region Fields

        private bool _isTrackingViewState;
        private StateBag _viewState;

        #endregion

        /// <summary>
        /// Sets the instance data as been updated.
        /// </summary>
        internal void SetDirty()
        {
            this.ViewState.SetDirty(true);
        }

        object ICloneable.Clone()
        {
            HierListItemBinding binding = new HierListItemBinding();
            binding.DataMember = this.DataMember;
            binding.Depth = this.Depth;
            binding.Enabled = this.Enabled;
            binding.EnabledField = this.EnabledField;
            binding.FormatString = this.FormatString;
            binding.NavigateUrl = this.NavigateUrl;
            binding.NavigateUrlField = this.NavigateUrlField;            
            binding.Selectable = this.Selectable;
            binding.SelectableField = this.SelectableField;            
            binding.Target = this.Target;
            binding.TargetField = this.TargetField;
            binding.Text = this.Text;
            binding.TextField = this.TextField;
            binding.ToolTip = this.ToolTip;
            binding.ToolTipField = this.ToolTipField;
            binding.Value = this.Value;
            binding.ValueField = this.ValueField;
            return binding;
        }

        void IStateManager.LoadViewState(object state)
        {
            if (state != null)
            {
                ((IStateManager)this.ViewState).LoadViewState(state);
            }
        }

        object IStateManager.SaveViewState()
        {
            if (this._viewState != null)
            {
                return ((IStateManager)this._viewState).SaveViewState();
            }
            return null;
        }

        void IStateManager.TrackViewState()
        {
            this._isTrackingViewState = true;
            if (this._viewState != null)
            {
                ((IStateManager)this._viewState).TrackViewState();
            }
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            if (!string.IsNullOrEmpty(this.DataMember))
            {
                return this.DataMember;
            }
            return "TreeNodeBinding_EmptyBindingText";
        }

        // Properties
        /// <summary>
        /// Gets or sets the data member to bind to in the data source.
        /// </summary>
        /// <value>The data member.</value>
        [Category("Data"), 
        Description("The data member to bind to in the data source"), 
        DefaultValue("")]
        public string DataMember
        {
            get
            {
                object obj2 = this.ViewState["DataMember"];
                if (obj2 == null)
                {
                    return string.Empty;
                }
                return (string)obj2;
            }
            set
            {
                this.ViewState["DataMember"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the depth level of the list item from the root.
        /// </summary>
        /// <value>The depth.</value>
        [Category("Data"),
        Description("The depth level of the list item from the root."),  
        DefaultValue(-1), 
        TypeConverter("System.Web.UI.Design.WebControls.TreeNodeBindingDepthConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        public int Depth
        {
            get
            {
                object obj2 = this.ViewState["Depth"];
                if (obj2 == null)
                {
                    return -1;
                }
                return (int)obj2;
            }
            set
            {
                this.ViewState["Depth"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the enabled value to apply to the matching list item.
        /// </summary>
        /// <value>The enabled.</value>
        [Category("Databindings"),
        Description("The enabled value to apply to the matching list item."), 
        DefaultValue(null)]
        public bool? Enabled
        {
            get
            {
                object obj2 = this.ViewState["Enabled"];
                if (obj2 != null)
                {
                    return (bool)obj2;
                }
                return true;
            }
            set
            {
                this.ViewState["Enabled"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the data source field to use for the item Enabled property.
        /// </summary>
        /// <value>The enabled field.</value>
        [Category("DefaultProperties"), 
        Description("The data source field to use for the item Enabled property."), 
        DefaultValue(""), 
        TypeConverter("System.Web.UI.Design.DataSourceViewSchemaConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        public string EnabledField
        {
            get
            {
                object obj2 = this.ViewState["EnabledField"];
                if (obj2 != null)
                {
                    return (string)obj2;
                }
                return string.Empty;
            }
            set
            {
                this.ViewState["EnabledField"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the format applied to the item text.
        /// </summary>
        /// <value>The format string.</value>
        [Category("Databindings"), 
        Description("The format applied to the item text."),
        DefaultValue(""),
        Localizable(true)]
        public string FormatString
        {
            get
            {
                object obj2 = this.ViewState["FormatString"];
                if (obj2 == null)
                {
                    return string.Empty;
                }
                return (string)obj2;
            }
            set
            {
                this.ViewState["FormatString"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the URL to apply to the matching list item.
        /// </summary>
        /// <value>The navigate URL.</value>
        [Category("DefaultProperties"),         
        Description("The URL to apply to the matching list item."),
        UrlProperty, 
        Editor("System.Web.UI.Design.UrlEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)), DefaultValue("")]
        public string NavigateUrl
        {
            get
            {
                object obj2 = this.ViewState["NavigateUrl"];
                if (obj2 == null)
                {
                    return string.Empty;
                }
                return (string)obj2;
            }
            set
            {
                this.ViewState["NavigateUrl"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the data source field to use for the item NavigateUrl property.
        /// </summary>
        /// <value>The navigate URL field.</value>
        [Category("Databindings"),
        Description("The data source field to use for the item NavigateUrl property."), 
        DefaultValue(""), 
        TypeConverter("System.Web.UI.Design.DataSourceViewSchemaConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        public string NavigateUrlField
        {
            get
            {
                object obj2 = this.ViewState["NavigateUrlField"];
                if (obj2 == null)
                {
                    return string.Empty;
                }
                return (string)obj2;
            }
            set
            {
                this.ViewState["NavigateUrlField"] = value;
            }
        }

        /// <summary>
        /// Gets or sets whether the list item can be selected.
        /// </summary>
        /// <value>The selectable.</value>
        [Category("DefaultProperties"),
        Description("Whether the list item can be selected."),
        DefaultValue(true)]
        public bool? Selectable
        {
            get
            {
                object obj2 = this.ViewState["Selectable"];
                if (obj2 != null)
                {
                    return (bool)obj2;
                }
                return true;
            }
            set
            {
                this.ViewState["Selectable"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the selectable value to apply to the matching list item.
        /// </summary>
        /// <value>The selectable field.</value>
        [Category("Databindings"), 
        Description("MenuItemBinding_SelectableField"),
        DefaultValue("The selectable value to apply to the matching list item."), 
        TypeConverter("System.Web.UI.Design.DataSourceViewSchemaConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        public string SelectableField
        {
            get
            {
                object obj2 = this.ViewState["SelectableField"];
                if (obj2 != null)
                {
                    return (string)obj2;
                }
                return string.Empty;
            }
            set
            {
                this.ViewState["SelectableField"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the target to apply to the matching list item URL.
        /// </summary>
        /// <value>The target.</value>
        [Description("The target to apply to the matching list item URL."), 
        DefaultValue(""), 
        Category("DefaultProperties")]
        public string Target
        {
            get
            {
                object obj2 = this.ViewState["Target"];
                if (obj2 == null)
                {
                    return string.Empty;
                }
                return (string)obj2;
            }
            set
            {
                this.ViewState["Target"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the data source field to use for the item Target property.
        /// </summary>
        /// <value>The target field.</value>
        [Category("Databindings"),
        Description("The data source field to use for the item Target property."),
        DefaultValue(""), 
        TypeConverter("System.Web.UI.Design.DataSourceViewSchemaConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        public string TargetField
        {
            get
            {
                string str = (string)this.ViewState["TargetField"];
                if (str == null)
                {
                    return string.Empty;
                }
                return str;
            }
            set
            {
                this.ViewState["TargetField"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the text to apply to the matching list item.
        /// </summary>
        /// <value>The text.</value>
        [Category("DefaultProperties"),         
        Description("The text to apply to the matching list item."),
        DefaultValue(""), 
        Localizable(true)]
        public string Text
        {
            get
            {
                object obj2 = this.ViewState["Text"];
                if (obj2 == null)
                {
                    obj2 = this.ViewState["Value"];
                    if (obj2 == null)
                    {
                        return string.Empty;
                    }
                }
                return (string)obj2;
            }
            set
            {
                this.ViewState["Text"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the data source field to use for the item Text property.
        /// </summary>
        /// <value>The text field.</value>
        [Category("Databindings"), 
        Description("The data source field to use for the item Text property."),         
        DefaultValue(""),
        TypeConverter("System.Web.UI.Design.DataSourceViewSchemaConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        public string TextField
        {
            get
            {
                object obj2 = this.ViewState["TextField"];
                if (obj2 == null)
                {
                    return string.Empty;
                }
                return (string)obj2;
            }
            set
            {
                this.ViewState["TextField"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the tool tip to apply to the matching list item.
        /// </summary>
        /// <value>The tool tip.</value>
        [Category("DefaultProperties"), 
        Description("The tool tip to apply to the matching list item."), 
        DefaultValue(""), Localizable(true)]
        public string ToolTip
        {
            get
            {
                object obj2 = this.ViewState["ToolTip"];
                if (obj2 == null)
                {
                    return string.Empty;
                }
                return (string)obj2;
            }
            set
            {
                this.ViewState["ToolTip"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the data source field to use for the item ToolTip property.
        /// </summary>
        /// <value>The tool tip field.</value>
        [Category("Databindings"), 
        DefaultValue(""),         
        Description("The data source field to use for the item ToolTip property."), 
        TypeConverter("System.Web.UI.Design.DataSourceViewSchemaConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        public string ToolTipField
        {
            get
            {
                object obj2 = this.ViewState["ToolTipField"];
                if (obj2 == null)
                {
                    return string.Empty;
                }
                return (string)obj2;
            }
            set
            {
                this.ViewState["ToolTipField"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the value to apply to the matching list item.
        /// </summary>
        /// <value>The value.</value>
        [Category("DefaultProperties"), 
        Description("The value to apply to the matching list item."),         
        DefaultValue(""), 
        Localizable(true)]
        public string Value
        {
            get
            {
                object obj2 = this.ViewState["Value"];
                if (obj2 == null)
                {
                    obj2 = this.ViewState["Text"];
                    if (obj2 == null)
                    {
                        return string.Empty;
                    }
                }
                return (string)obj2;
            }
            set
            {
                this.ViewState["Value"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the data source field to use for the item Value property.
        /// </summary>
        /// <value>The value field.</value>
        [Category("Databindings"), 
        DefaultValue(""), 
        Description("The data source field to use for the item Value property."), 
        TypeConverter("System.Web.UI.Design.DataSourceViewSchemaConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        public string ValueField
        {
            get
            {
                object obj2 = this.ViewState["ValueField"];
                if (obj2 == null)
                {
                    return string.Empty;
                }
                return (string)obj2;
            }
            set
            {
                this.ViewState["ValueField"] = value;
            }
        }

        object IDataSourceViewSchemaAccessor.DataSourceViewSchema
        {
            get
            {
                return this.ViewState["IDataSourceViewSchemaAccessor.DataSourceViewSchema"];
            }
            set
            {
                this.ViewState["IDataSourceViewSchemaAccessor.DataSourceViewSchema"] = value;
            }
        }

        bool IStateManager.IsTrackingViewState
        {
            get
            {
                return this._isTrackingViewState;
            }
        }

        private StateBag ViewState
        {
            get
            {
                if (this._viewState == null)
                {
                    this._viewState = new StateBag();
                    if (this._isTrackingViewState)
                    {
                        ((IStateManager)this._viewState).TrackViewState();
                    }
                }
                return this._viewState;
            }
        }
    }
}
